package engine.game.creature {
	
	import engine.base.GameBase;
	import engine.game.creature.states.*;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	* ...
	* @author Jon Smelquist
	*/
	public class Creature extends GameBase {
		
		protected var _state:ICreatureState;
		protected var _standingState:ICreatureState;
		protected var _creepingState:ICreatureState;
		
		public function get standingState():ICreatureState { return _standingState; };
		public function get creepingState():ICreatureState { return _creepingState; };
		
		public function Creature(imageSource:Class, position:Point) {
			// Initialize the creature with a 60ms update rate
			super(60);
			// Animation column selection will be "states" of a player in a direction
			_animCol = 0;
			// Animation row selection dictate the "position" of the player (NWSE Direction)
			_animRow = 0;
			
			_position = position;
			
			// Set up states
			_standingState = new StandingState(this);
			_creepingState = new CreepingState(this);
			
			var source:Bitmap = new imageSource();
			_source = new BitmapData( source.width, source.height, true, 0x00000000 );
			_source.draw( source );
			_display = new BitmapData( 32, 48, true, 0x00000000 );
			
			_state = _creepingState;
		}
		
		public function setCreaturePosition(position:Point, direction:int):void {
			_position = position;
			_animRow = direction;
		}
		
		public function stand():void {
			_state.stand();
		}
		
		public function changeState(state:ICreatureState):void
		{
			_state = state;
		}
		
		public override function draw(surface:BitmapData, camRect:Rectangle):void
		{
			surface.copyPixels( _display, _display.rect, new Point( (_position.x - camRect.x), (_position.y - camRect.y) + 12 ));
		}
		
		protected override function update():void
		{
			_state.update();
			// if state is not within repeating walking states (0-3) do not perform modulus modifications
			if (_animCol <= 3)
				_display.copyPixels(_source, new Rectangle((_animCol % 3) * _display.width, (_animRow * _display.height), _display.width, _display.height), new Point(0, 0));
			else	
				_display.copyPixels(_source, new Rectangle((_animCol * _display.width), (_animRow * _display.height), _display.width, _display.height), new Point(0, 0));
		}
		
		public function creep():void {	
			_state.creep();
		}
		
	}
	
}